<!DOCTYPE html>
<html>
<head>
    <title>账户配置</title>
    <#include "/header.html">
    <link
            href="${request.contextPath}/statics/plugins/jquery-layout/jquery.layout-latest.css"
            rel="stylesheet" />
 </head>
<body>


<div id="rrapp" v-cloak class="container-div ui-layout-center">
    <div v-show="showList">
        <!--<span id="directory" class="switch-on"></span>-->
        <div class="grid-btn">
            <div class="form-group col-sm-2">
                <input type="text" class="form-control" v-model="q.username"
                       @keyup.enter="query" placeholder="账号名称">
            </div>
            <a class="btn btn-default" @click="query">查询</a>
            <a class="btn btn-primary" @click="add"><i class="fa fa-plus"></i>&nbsp;新增</a>
            <a class="btn btn-primary" @click="update"><i
            class="fa fa-pencil-square-o"></i>&nbsp;修改</a>
            <a class="btn btn-danger" @click="del"><i class="fa fa-trash-o"></i>&nbsp;删除</a>
</div>


<div class="col-sm-12 select-info table-striped">
    <table id="bootstrap-table" data-mobile-responsive="true"></table>
</div>
</div>

<div v-show="!showList" class="panel panel-default"  >
    <div class="panel-heading">{{title}}</div>
    <form class="form-horizontal" id="form">
        <div class="form-group">
            <div class="col-sm-2 control-label">账号名称</div>
            <div class="col-sm-10">
                <input type="text" class="form-control" v-model="user.username" placeholder="账号名称"/>
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-2 control-label">密码</div>
            <div class="col-sm-10">
                <input type="password" class="form-control" v-model="user.password" placeholder="密码"/>
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-2 control-label">密钥</div>
            <div class="col-sm-10">
                <input type="text" class="form-control" v-model="user.token" placeholder="密钥"/>
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-2 control-label">是否拉取</div>
            <div class="col-sm-10">
                <input type="checkbox" class="checke" id="isPull">
                <!--<input type="checkbox" class="checke" id="isPull" v-if="user.isPull==1">-->
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-2 control-label"></div>
            <input type="button" ref="submitBtn" class="btn btn-primary" @click="saveOrUpdate" value="确定"/>
            &nbsp;&nbsp;<input type="button" class="btn btn-warning" @click="reload" value="返回"/>
        </div>
    </form>

</div>


</div>


<script
        src="${request.contextPath}/statics/plugins/jquery-layout/jquery.layout-latest.js"></script>


<!-- <script src="${request.contextPath}/statics/js/modules/sys/user.js?_${.now?long}"></script>
-->
<script>


    $(document)
        .ready(
            function() {
                $('body').layout({
                    west__size : 185
                });
                /* queryUserList();*/
                $('#bootstrap-table')
                    .bootstrapTable(
                        {
                            method : 'post', // 服务器数据的请求方式 get or post
                            contentType : "application/json",
                            url : baseURL + 'baiduaccount/findBaiduAccountList', // 服务器数据的加载地址
                            showRefresh : true,
                            showToggle : true,
                            showColumns : true,
                            //showExport :true,
                            sortable: true,
                            sortName:"userId",// 排序列名称
                            sortOrder: "desc",
                            iconSize : 'outline',
                            toolbar : '#exampleToolbar',
                            striped : true, // 设置为true会有隔行变色效果
                            dataType : "json", // 服务器返回的数据类型
                            pagination : true, // 设置为true会在底部显示分页条
                            // queryParamsType : "limit",
                            // //设置为limit则会发送符合RESTFull格式的参数
                            singleSelect : false, // 设置为true将禁止多选
                            // contentType : "application/x-www-form-urlencoded",
                            // //发送到服务器的数据编码类型
                            pageSize : 10, // 如果设置了分页，每页数据条数
                            pageNumber : 1, // 如果设置了分布，首页页码
                            // search : true, // 是否显示搜索框
                            //showColumns : false, // 是否显示内容下拉框（选择显示的列）
                            sidePagination : "server", // 设置在哪里进行分页，可选值为"client" 或者
                            // "server"
                            queryParams : function(params) {
                                //console.log(params)
                               return {
                                   // 说明：传入后台的参数包括offset开始索引，limit步长，sort排序列，order：desc或者,以及所有列的键值对
                                   limit : params.limit,
                                   page : (params.offset/params.limit) + 1,
                                   username : vm.q.username,
                                   // sidx: params.sort,      //排序列名
                                   //order: params.order //排位命令（desc，asc）
                                   /* sort:"userId",
                                   order:"desc" */
                               };
                            },
                            // //请求服务器数据时，你可以通过重写参数的方式添加一些额外的参数，例如 toolbar 中的参数 如果
                            // queryParamsType = 'limit' ,返回参数必须包含
                            // limit, offset, search, sort, order 否则, 需要包含:
                            // pageSize, pageNumber, searchText, sortName,
                            // sortOrder.
                            // 返回false将会终止请求
                            columns : [
                                {
                                    checkbox : true
                                },
                                {
                                    field : 'username',
                                    align:'center',
                                    title : '账户名称'
                                },
                                {
                                    field : 'password', // 列字段名
                                    align:'center',
                                    title : '密码',// 列标题
                                },
                                {
                                    field : 'token',
                                    align:'center',
                                    title : '密钥'
                                },
                                {
                                    field : 'isPull',
                                    align:'center',
                                    title : '是否拉取',
                                    formatter : function(
                                        value, row,
                                        index) {
                                        //console.log(row)
                                            var e = !row.isPull?'<input sid="'+ row.id +'" type="checkbox" class="checke" checked="checked">':'<input sid="'+ row.id +'" type="checkbox" class="checke">';
                                           return e;
                                    }
                                },
                               {
                                    title : '操作',
                                    field : 'id',
                                    width:330,
                                    align : 'center',
                                    formatter : function(
                                        value, row,
                                        index) {
                                        var e = '<a  class="btn btn-primary btn-sm '
                                            + ''
                                            + '" href="#" mce_href="#" title="编辑" onclick="edit(\''
                                            + row.id
                                            + '\')"><i class="fa fa-edit " style="margin-right: 5px"></i>编辑</a> ';
                                        var d = '<a class="btn btn-danger btn-sm '
                                            + ''
                                            + '" href="#" title="删除"  mce_href="#" onclick="remove(\''
                                            + row.id
                                            + '\')"><i class="fa fa-remove" style="margin-right: 5px"></i>删除</a> ';
                                        return e + d;
                                    }
                                } ]
                        });
            });
    var vm = new Vue({
        el : '#rrapp',
        data : {
            q : {
                username : ''
            },
            showList : true,
            title : null,
            roleList : {},
            user : {
                 username:'',
                 password:'',
                 token:'',
                 isPull:1
            }
        },
        methods : {
            query : function() {
                vm.reload();
            },
            add : function() {
                vm.showList = false;
                vm.title = "新增";
                vm.roleList = {};
                vm.user = {
                    username:'',
                    password:'',
                    token:'',
                    isPull:1
                };
            },
            update : function() {
                // console.log(11111)
                vm.user = {
                    username:'',
                    password:'',
                    token:'',
                    isPull:1
                };
                var userId = getSelectedRow();
                if (userId == null) {
                    return;
                }
                vm.showList = false;
                vm.title = "修改";
                vm.getUser(userId.id);
            },
            del : function() {
                var delArr = [];
                var userIds = getSelectedRows();
                if (userIds == null) {
                    return;
                }
               // console.log(userIds);
                userIds.map(function (item) {
                    delArr.push(item.id)
                });
               // return false;
                confirm('确定要删除选中的记录？', function() {
                    $.ajax({
                        type : "POST",
                        url : baseURL + "baiduaccount/deleteBaiduAccount",
                        contentType : "application/json",
                        data : JSON.stringify({id:delArr}),
                        success : function(r) {
                            if (r.code == 0) {
                                alert('操作成功', function() {
                                    vm.reload();
                                });
                            } else {
                                alert(r.msg);
                            }
                        }
                    });
                });
            },
            saveOrUpdate : function() {
                var url = vm.user.id == null ? "baiduaccount/saveBaiduAccount"
                    : "baiduaccount/updateBaiduAccount";
                //console.log(vm.user.platformId);
                var btn = vm.$refs.submitBtn;
                if(checkIsBank(vm.user.username) || checkIsBank(vm.user.password) || checkIsBank(vm.user.token)){
                    alert("全部为必填字段!");
                    return false;
                }
                btn.disabled = true;
                vm.user.isPull = $("#isPull").is(":checked")?0:1;
                $.ajax({
                    type : "POST",
                    url : baseURL + url,
                    contentType : "application/json",
                    data : JSON.stringify(vm.user),
                    success : function(r) {
                        btn.disabled = false;
                        if (r.code === 0) {
                            alert('操作成功', function() {
                                vm.reload();
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            },
            getUser : function(id) {
                $.ajax({
                    type : "POST",
                    url : baseURL + "baiduaccount/findAccountInfoById",
                    contentType : "application/json",
                    data : JSON.stringify({id:id}),
                    success : function(r) {
                        if (r.code === 0) {
                            var data = r.result;
                            vm.user.isPull = data.isPull;
                            data.isPull?$("#isPull").prop("checked",false):$("#isPull").prop("checked",true);
                            vm.user.username = data.username;
                            vm.user.password = data.password;
                            vm.user.token = data.token;
                            vm.user.id = data.id;
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            },
            reload : function() {
                vm.showList = true;
                var opt = {
                    url: baseURL + 'baiduaccount/findBaiduAccountList',
                    contentType : "application/json",
                    silent: true,
                    query:{
                        limit : 10,
                        page : 1,
                        platformName : vm.q.platformName
                    }
                };

                $('#bootstrap-table').bootstrapTable('refresh',
                    opt);

            }
        }
    });

   $(document).on("click",".checke",function () {
       //console.log( $(this).is(":checked"))
       //console.log($(this).attr("sid"));
       var sid = $(this).attr("sid");
       if(sid != undefined){
           var params = {
               id:sid,
               isPull:$(this).is(":checked")?0:1
           };
           $(this).attr("disabled",true);
           $.ajax({
               type : "POST",
               url : baseURL + "baiduaccount/updateAccountPullStatus",
               contentType : "application/json",
               data : JSON.stringify(params),
               success : function(r) {
                   if (r.code === 0) {
                       $(this).attr("disabled",false);
                       alert('操作成功', function() {
                           vm.reload();
                       });
                   } else {
                       alert(r.msg);
                   }
               }
           });
       }
   });


    function edit(id) {
        if (id == null) {
            return;
        }
        //console.log(2222)
        vm.user = {
            username:'',
            password:'',
            token:'',
            isPull:1
        };
        vm.showList = false;
        vm.title = "修改";
        vm.getUser(id);
    }

    function remove(id) {
        confirm('确定要删除选中的记录？', function() {
            $.ajax({
                type : "POST",
                url : baseURL + "baiduaccount/deleteBaiduAccount",
                contentType : "application/json",
                data : JSON.stringify({id:[id]}),
                success : function(r) {
                    if (r.code == 0) {
                        alert('操作成功', function() {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        });
    }
</script>


</body>
</html>